﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Aandelendelen.News;

namespace Aandelendelen.Controls
{
	public partial class NewsItemControl : BaseUserControl
	{
		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.SetNewsItem();
		}

		private void SetNewsItem()
		{
			string sindex = this.Request["index"];

			if (sindex == null)
			{
				this.lblContent.Text = "Nieuws item niet gevonden";

				return;
			}

			int index;
			try
			{
				index = Convert.ToInt32(sindex);
			}
			catch
			{
				index = -1;
			}

			NewsItemCollection newsItems = NewsCache.GetNews();

			if (index >= newsItems.Count || index < 0)
			{
				this.lblContent.Text = "Nieuws item niet gevonden";

				return;
			}

			this.DisplayNewsItem(newsItems[index]);
		}

		private void DisplayNewsItem(NewsItem item)
		{
			HtmlMeta meta = new HtmlMeta();
			meta.Name = "description";
			meta.Content = "Nieuwsbericht: " + item.Title;

			this.Page.Header.Controls.Add(meta);
			this.Page.Title = "AandelenDelen - " + item.Title;

			this.lblTitle.Text = item.Title;
			this.lblDate.Text = item.Date.ToString("dddd, dd MMM yyyy HH:mm");
			this.lblContent.Text = item.Content;
		}
	}
}